package merlin.managers;

import java.sql.*;
import java.util.*;

import merlin.beans.*;
import merlin.dao.*;
import merlin.utils.*;

public class MSALControlSystemManager{

	/**
	 * Function to verify user credentials for the MSAL Control System.
	 */
	public static boolean loginForControlSystem(String username, String password){
		ConnectionPool pool = null;
		Connection con = null;

		boolean success = false;
		try{
			pool = ConnectionPool.getConnectionPool("website");
			con = pool.getConnection();

			SystemConfigurationDAO scdao = new SystemConfigurationDAO(con);

			Map mapConfigs = scdao.selectSystemConfigurationRecords(null,null);
			success = password.equals(mapConfigs.get(username));

		}catch(Exception e){
			e.printStackTrace();
		}finally{
			pool.returnConnection(con);
			con=null;
		}

    	return success;
	}

	public static int unsubscribeList(String emailIds){
		//System.out.println(emailIds);

		emailIds = emailIds.toLowerCase();

		List lstMailIds = new ArrayList();
		//clean the data.
		String mailIds[] = emailIds.split("\n");	// first dissect on lines.
		// now tab-separate the data.
		for(int i=0;i<mailIds.length;i++){
        	String ids[] = mailIds[i].split("\t");  // then on tabs.
        	for(int j=0;j<ids.length;j++){
        		String id = ids[j].trim();
        		if(id.length() > 0){
		        	lstMailIds.add(id);
				}
	        }
		}

		int result = 0;

		if(lstMailIds.size() > 0){
			ConnectionPool pool = null;
			Connection con = null;

			try{
				pool = ConnectionPool.getConnectionPool("website");
				con = pool.getConnection();

	            CompanyWebsiteDetailsDAO cwdao = new CompanyWebsiteDetailsDAO(con);

				result = cwdao.unsubscribeEmailIds(lstMailIds);
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				pool.returnConnection(con);
				con=null;
			}
		}
		return result;
	}

	public static void generateMailingList(boolean selectYoungest,int reportNumber){
		System.out.println(selectYoungest + " : " + reportNumber);
		int recordLimit = Integer.parseInt(ApplicationResources.getInstance().getProperty("cutoffListForMailingList"));
		new Thread(new MailingListGenerator(selectYoungest,reportNumber, recordLimit)).start();
	}

	public static int[] cleanWebsiteNamesAndEmailAddresses(){
		int result[] = new int[2];

		ConnectionPool pool = null;
		Connection con = null;

		try{
			pool = ConnectionPool.getConnectionPool("website");
			con = pool.getConnection();

            CompanyWebsiteDetailsDAO cwdao = new CompanyWebsiteDetailsDAO(con);
            con.setAutoCommit(false);
			result[0] = cwdao.cleanWebsiteNames();
			result[1] = cwdao.cleanEmailAddresses();
			con.commit();
		}catch(Exception e){
			e.printStackTrace();
			try{
				con.rollback();
			}catch(Exception ee){
				ee.printStackTrace();
			}
		}finally{
			pool.returnConnection(con);
			con=null;
		}
		return result;
	}

	public static EmailListingBean[] getEmailListingBeans(){
		EmailListingBean[] result = null;

		ConnectionPool pool = null;
		Connection con = null;

		try{
			pool = ConnectionPool.getConnectionPool("website");
			con = pool.getConnection();

            CompanyWebsiteDetailsDAO cwdao = new CompanyWebsiteDetailsDAO(con);
			result = cwdao.getEmailListingBeans();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			pool.returnConnection(con);
			con=null;
		}
		return result;
	}

	public static EmailListingBean getEmailListingBeanByListingId(int listingId){
		EmailListingBean result = null;

		ConnectionPool pool = null;
		Connection con = null;

		try{
			pool = ConnectionPool.getConnectionPool("website");
			con = pool.getConnection();

            CompanyWebsiteDetailsDAO cwdao = new CompanyWebsiteDetailsDAO(con);
			result = cwdao.getEmailListingBeanByListingId(listingId);
			cwdao.updateEmailListingDownloadDate(listingId);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			pool.returnConnection(con);
			con=null;
		}
		return result;
	}

	public static int deleteEmailListings(String listingIds){
		int result = 0;
		ConnectionPool pool = null;
		Connection con = null;

		try{
			pool = ConnectionPool.getConnectionPool("website");
			con = pool.getConnection();

            CompanyWebsiteDetailsDAO cwdao = new CompanyWebsiteDetailsDAO(con);
			result = cwdao.deleteEmailListings(listingIds);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			pool.returnConnection(con);
			con=null;
		}

		return result;
	}
}